package 数组;


import java.util.Scanner;

/**
 * @Author Weton Li
 * @Date 2021/4/19 22:26
 */
public class 合并有序数组 {
    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int p1 = m - 1, p2 = n - 1;
        int tail = m + n - 1;
        int cur;
        while (p1 >= 0 || p2 >= 0) {
            if (p1 == -1) {
                cur = nums2[p2--];
            } else if (p2 == -1) {
                cur = nums1[p1--];
            } else if (nums1[p1] > nums2[p2]) {
                cur = nums1[p1--];
            } else {
                cur = nums2[p2--];
            }
            nums1[tail--] = cur;
        }
    }

    public static void method(int[] nums1,int m,int[] nums2,int n){
        int p1 = m -1;
        int p2 = n -1;
        int tail = m+n-1;

        int curNum;
        while (p1>=0 ||p2>=0){

            // 必须先判断两个指针 指到-1位置的情况，否则会报错！
            if (p2 == -1){
                curNum = nums1[p1];
                p1--;
            } else if (p1 == -1) {
                curNum = nums2[p2];
                p2--;
            } else if (nums1[p1] > nums2[p2]) {
                curNum = nums1[p1];
                p1--;
            }else {
                curNum = nums2[p2];
                p2--;

            }
            nums1[tail] = curNum;
            tail--;
        }
    }


    public static void main(String[] args) {
        Scanner scanner1 = new Scanner(System.in);
        Scanner scanner2 = new Scanner(System.in);
        Scanner scanner3 = new Scanner(System.in);
        Scanner scanner4 = new Scanner(System.in);
        int m = scanner1.nextInt();
//        System.out.print(",n=");
        int n = scanner2.nextInt();
        System.out.printf("m=%d,n=%d",m,n);

        int[] n1 = new int[m];
        int[] n2 = new int[n];
        for (int i = 0; i < m; i++) {
            n1[i] = scanner3.nextInt();
        }
        for (int j = 0; j < n; j++) {
            n2[j] = scanner4.nextInt();
        }
        method(n1,m,n2,n);
        for (int i : n1) {
            System.out.print(i+"\t");
        }
//        int[] input1 = {1,2,3,0,0};
//        int[] input2 = {2,5};
//m=2,n=2
//1,3
//2,4

        // 1 2 3 4
//        method(input1,3,input2,2);
//        for (int i : input1) {
//            System.out.print(i+"\t");
//        }
    }
}
